"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrefabEditor = void 0;
var fs_1 = __importDefault(require("fs"));
var yaml_1 = __importDefault(require("yaml"));
var PrefabEditor = /** @class */ (function () {
    function PrefabEditor() {
        this.elements = [];
        this.elemMap = {};
        yaml_1.default.scalarOptions.int.asBigInt = true;
    }
    PrefabEditor.prototype.readGUID = function (filePath) {
        var fileContent = fs_1.default.readFileSync(filePath, 'utf-8');
        var meta = yaml_1.default.parse(fileContent);
        this.activeToggleGUID = meta.guid;
    };
    PrefabEditor.prototype.edit = function (filePath) {
        var fileContent = fs_1.default.readFileSync(filePath, 'utf-8');
        this.elements.length = 0;
        this.elemMap = {};
        var fileLines = fileContent.split(/[\r\n]+/);
        var elem;
        // 前两行为tag跳过
        for (var i = 2, len = fileLines.length; i < len; i++) {
            var line = fileLines[i];
            var docMatchRst = line.match(/--- !u!(\d+) &(\d+)/);
            if (docMatchRst) {
                // 匹配到新元素
                if (elem) {
                    elem.range.end = i;
                }
                elem = this.newElement(Number(docMatchRst[1]), docMatchRst[2], i + 1);
                this.elemMap[elem.fileID] = elem;
                this.elements.push(elem);
            }
            else {
                if (elem) {
                    elem.rawContents += line + '\n';
                }
            }
        }
        // 检查seletable
        fileContent = this.removeSeletable(fileContent);
        fs_1.default.writeFileSync(filePath, fileContent, 'utf-8');
    };
    PrefabEditor.prototype.newElement = function (yamlID, fileID, start) {
        return {
            yamlID: yamlID,
            fileID: fileID,
            range: {
                start: start,
                end: start
            },
            rawContents: ''
        };
    };
    PrefabEditor.prototype.removeSeletable = function (fileContent) {
        for (var i = 0, len = this.elements.length; i < len; i++) {
            var elem = this.elements[i];
            if (elem.rawContents.includes('m_Script: {fileID: 11500000, guid: ' + this.activeToggleGUID + ', type: 3}')) {
                // 这是ActiveToggle
                var normalRst = elem.rawContents.match(/normalState: \{fileID: (\d+)\}/);
                var normalFileID = normalRst[1];
                if (normalFileID != '0') {
                    fileContent = this.removeOneSelectable(normalFileID, fileContent);
                }
                var selectedRst = elem.rawContents.match(/selectedState: \{fileID: (\d+)\}/);
                var selectedFileID = selectedRst[1];
                if (selectedFileID != '0') {
                    fileContent = this.removeOneSelectable(selectedFileID, fileContent);
                }
            }
        }
        return fileContent;
    };
    PrefabEditor.prototype.removeOneSelectable = function (seletableID, fileContent) {
        var elem = this.elemMap[seletableID];
        console.assert(elem != null, "seletable not exists: " + seletableID);
        if (!elem.obj) {
            elem.obj = yaml_1.default.parse(elem.rawContents);
        }
        // 删除Selectable组件
        fileContent = fileContent.replace('--- !u!' + elem.yamlID + ' &' + elem.fileID + '\n' + elem.rawContents, '');
        // 删除组件引用
        var reg = new RegExp('  - component: \\{fileID: ' + elem.fileID + '\\}\\r?\n', 'g');
        fileContent = fileContent.replace(reg, '');
        // 替换组件引用
        fileContent = fileContent.replace(seletableID, elem.obj.MonoBehaviour.m_GameObject.fileID);
        return fileContent;
    };
    return PrefabEditor;
}());
exports.PrefabEditor = PrefabEditor;
